<?php
/**
 * Displays pages for static content
 *
 * ACCESS: PUBLIC
 *
 * @author David Driscoll
 * @version 1.0
 */
class Drawing extends Controller {
	
	/**
	 * Class constructor
	 */
	function __construct() {
		parent::__construct();
	}
	
	/**
	 * This displays the form and then the welcome page after a successful post
	 * 
	 */
	function index() {
		$this->load->library('validation');
		
		$data = $this->_buildContent();
		$data['pagetype'] = 'nobg';
		
		$rules['name'] = "trim|required";
		$rules['email'] = "trim|required|valid_email|callback_checkEmail";
		$rules['token'] = "trim|required|numeric|exact_length[6]|callback_checkToken";
		$rules['class'] = "trim|required";
		$rules['phone'] = 'trim|numeric|exact_length[10]';
		$rules['dorm'] = 'trim|alpha_numeric|max_length[12]';
		
		$this->validation->set_rules($rules);
		
		$fields['name']	= 'Name';
		$fields['email'] = 'Email Address';
		$fields['token'] = 'Token';
		$fields['class'] = 'Class';
		$fields['phone'] = 'Phone';
		$fields['dorm'] = 'Dorm';
		$fields['contact'] = 'Contact';
		
		$this->validation->set_fields($fields);
		
		$this->validation->set_error_delimiters('<div class="error">','</div>');
		
		if($this->validation->run() == FALSE) {
			$this->load->view('drawing/form',$data);
		} else {
			$this->load->model('DrawingModel');
			
			$post = array(
				'name' => $this->input->post('name',TRUE),
				'email' => $this->input->post('email', TRUE),
				'token' => $this->input->post('token', TRUE),
				'class' => $this->input->post('class', TRUE),
				'phone' => $this->input->post('phone', TRUE),
				'dorm' => $this->input->post('dorm', TRUE),
				'contact' => '',
			);
			
			if($this->input->post('contact', TRUE)) {
				$post['contact'] = implode(',',$this->input->post('contact', TRUE));
			}
			
			$this->DrawingModel->insertDrawing($post);
			
			$this->load->view('drawing/thanks', $data);
		}
	}
	
	/**
	 * This shows the report only to admin and staff
	 * 
	 */
	function report() {
		$this->badger->checkAllow('admin staff');
		$this->load->model('DrawingModel');
		$this->load->library('pagination');

		$data = $this->_buildContent();
		$data['pagetype'] = 'nobg';
		
		$data['entries'] = $this->DrawingModel->getAllEntries();
		
		$data['config']['base_url'] = site_url() . 'drawing/report/';
		$data['config']['total_rows'] = $data['entries']->num_rows;
		$data['config']['per_page'] = '20';

		$this->pagination->initialize($data['config']);

		$this->load->view('drawing/report' , $data);
	}
	
	/**
	 * validation to check to see if the email has already been used
	 * 
	 */
	function checkEmail($email) {
		$this->load->model('DrawingModel');
		
		if($this->DrawingModel->getEmail($email)->num_rows > 0) {
			$this->validation->set_message('checkEmail', 'Sorry this email has already used in the drawing.');
			return false;
		} else {
			return true;
		}
	}
	
	/**
	 * validation to check to see if the token has already been used
	 * 
	 */
	function checkToken($token) {
		$this->load->model('DrawingModel');
		
		if($this->DrawingModel->getToken($token)->num_rows > 0) {
			$this->validation->set_message('checkToken', 'Sorry this token has already used in the drawing.');
			return false;
		} else {
			return true;
		}
	}
	
	/*
	 * Supplies user-specific information to the interface for customization
	 * REQUIRED DATA ARRAY PARAMS:
	 * pagetype = page, page-admin, nobg
	 * loggedin = isset
	 */
	function _buildContent() {
		$data = array();
		if ($this->session->flashdata('msg_info')) $data['msg_info'] = $this->session->flashdata('msg_info');
		if ($this->session->flashdata('msg_error'))	$data['msg_error'] = $this->session->flashdata('msg_error');
		
		if ($this->session->userdata('loggedin')) {
			$this->load->model('UserModel');
			$user = $this->UserModel->getUserByName($this->session->userdata('username'));
			
			$data['con_user'] = $user;
			$data['loggedin'] = TRUE;
			
			//is admin?
			if ($this->UserModel->isUserInGroup($user->userid,$this->UserModel->getGroupID('admin'))) {
				$data['admin_tab'] = TRUE;
				$data['drawing_report'] = TRUE;
			}
			
			//is prayer team?
			if ($this->UserModel->isUserInGroup($user->userid,$this->UserModel->getGroupID('lg_pop'))) {
				$data['prayer_tab'] = TRUE;
			}
		}
		
		$data['drawingHeader'] = "XA Concert Drawing";
		
		return $data;
	}
}
?>
